/*
 * Creator:     ML
 * CreatDate:   2019-10-25
 * descripts：  国家术语集ICD 对照表
 * */
import React from 'react';
import { Form, Row, Col, Input,message,Select, Button, Modal, Table, Pagination } from 'antd';
import { $http } from 'containers/config/https';
import CommonPagination from 'components/pagination';


const FormItem = Form.Item;
const formItemNumber = {
  labelCol: { span: 4 },
  wrapperCol: { span: 20 },
}
const TerminologyRAndNnaModals = Form.create()(
  class TerminologyRAndNnaModals extends React.Component {
    constructor(props) {
      super(props)
      this.state = {
        total: 0,//表格总数据
        tableData: [],//表格数据
        parms: {},//单行信息
        page: 1,//页数
        pageSize: 50,//显示数
        loading: false,//确定按钮 loading
        visible: false,//查找病人弹窗
        rowId: "",//控制行高亮
        checkStaus: false,//是否点击行
        findInfo:'请输入查询条件',//查询提示信息
        search:"",
        size:"big"
      }
    }
    componentDidMount() {
      this.props.onRef(this);
    }
    handleOk = (e) => {
      this.setState({visible: false,page:1,pageSize:50})
    }
    showModal = (v) => {
      this.setState({
        visible: true,
        v,
      });
      this.getTableData(v)
    }
    handleCancel = () => {
      // this.setState({ visible: false, checkStaus: false, tableData: [], loading: false, rowId: '', total: 0, page: 1, pageSize: 10 ,findInfo:'请输入查询条件'});
      this.setState({visible: false,page:1,pageSize:50},()=>{
        this.onShowSizeChange("1",this.state.pageSize)
      })
      this.props.handleClear()
    }
    // 修改page和pageSize 跳转至---
    onChange = (e, pageSize) => {
      this.setState({ page: e, pageSize: this.state.pageSize }, () => {
        // 重新调用查询方法
        let { v} = this.state;
        this.getTableData(v);
      })
    }
    //下拉选择页数
    onShowSizeChange = (page, pageSize) => {
      this.setState({ page: 1, pageSize: pageSize }, () => {
        // 重新调用查询方法
        let { v } = this.state;
        this.getTableData(v,"search");
      })
    }
    // 显示分页总条数
    showTotal(total) {
      return `共 ${total} 条`;
    }
    //点击Row
    onClickRow = (record) => {
      
      return {
        onClick: () => {
          // console.log("rerere",record)
          this.setState({
            parms: record,
            checkStaus: true
          })
          if (this.state.rowId === "") {
            this.setState({
              rowId: record.id,
            });
          } else {
            if (this.state.rowId !== record.id) {
              this.setState({
                rowId: record.id,
              });
            } else {
              this.setState({
                rowId: "",
                detailInfo: {},
              });
            }
          }

        },
        onDoubleClick: () => {
          this.setState({
            rowId: record.id,
            parms: record,
            checkStaus: true
          })
          this.handleRow()
        }
      };
    }
    //设置点击row颜色
    setRowClassName = (record) => {
      return record.id === this.state.rowId ? 'clickRowStyle' : '';
    }
    resetEditFieldData = () => {
      this.setState({
        rowId: "",
        detailInfo: {}
      });
    }
    // 请求列表数据
    getTableData = (v,e) => {
      let { page, pageSize, } = this.state;
      let data = {}
      data = {
        params: [{
          inputValue:v,
        }],
        pagination: [{
          pageSize: pageSize,
          currentPage: page,
        }]
      }
      $http.post('urlS', this, {
        code: '05100262',
        data: data,
        success: function (res) {
          if (+res.errorCode === 0 && res.result) {
            for (var i = 0; i < res.result.Data.length; i++) {
              res.result.Data[i]["key"] = i
            }
            this.setState({ tableData: res.result.Data, total: res.result.TotalCount,search:e,
              rowId:res.result.Data&&res.result.Data.length!=0?res.result.Data[0].id:"",
            })
          } else {
          
          }
        }
      })
    }
    //选中行--确认
    handleRow = () => {
      // console.log("this.state.parms",this.state.parms)
      this.handleCancel()
      this.props.handleParms(this.state.parms)
    }
     //病人列表页面移动
     handleOnKeydown = (e) => {
      // console.log("----------",this.state.patientInfo)
      const {tableData} = this.state
      var e = window.event || e;
      let id = '';
      let oneArr = tableData&&tableData.length > 0 ? tableData : [];
      switch (e.keyCode) {
      case 13:
          e.preventDefault();
          if (this.state.rowId) {
            this.setState({
              visible: false,
            })
              let obj = {}
              for(var i = 0; i < oneArr.length; i++) {
                  if (oneArr[i].id === this.state.rowId) {
                      obj = i > 0 ? oneArr[i] : oneArr[0];
                      break;
                  }
              }
              // console.log("当前",obj)
              this.setState({
                visible: false,
                rowId: obj.id, 
                parms: obj,
                },()=>{
                  this.handleRow()
                })
                
              // this.checkNowData(obj)
              // this.confirmSelection(obj);
          } else {
              // message.info("请选择一条需要操作的数据")
          }
          break;
      case 38: //上
          if (this.state.rowId) {
              for(var i = 0; i < oneArr.length; i++) {
                  if (oneArr[i].id === this.state.rowId) {
                      id = i > 0 ? oneArr[i - 1].id : oneArr[0].id;
                      break;
                  }
              }
              this.setState({rowId: id})
          } else {
              this.setState({rowId: oneArr[0].id})
          }
          break;
      case 40: //下
          if (this.state.rowId) {
              for(let i = 0; i < oneArr.length; i++) {
                oneArr[i].disblock = false
                  if (oneArr[i].id === this.state.rowId) {
                      id = i + 1 >= oneArr.length ? oneArr[oneArr.length - 1].id : oneArr[i + 1].id;
                      break;
                  }
              }
              this.setState({rowId: id})
          } else {
              this.setState({rowId: oneArr[0].id})
          }
          break;
      }
  }
    render() {
      const { getFieldDecorator } = this.props.form;
      const { total, checkStaus, } = this.state
      const columns = [{
        key: '1',
        title: '代码',
        dataIndex: 'code',
        width: '15%',
        align: "center",
      }, {
        key: '2',
        title: '描述',
        dataIndex: 'descripts',
        width: '15%',
        align: 'center'
      },]
      return (
        <div
        tabIndex={1}
        onKeyDown={this.handleOnKeydown}
        style={{outline: 'none'}}
        >
        <Modal
          className='findPatients'
          width='1000px'
          visible={this.state.visible}
          title="术语集ICD"
          onOk={this.handleOk}
          onCancel={this.handleCancel}
          footer={[
            <Button key="submit" type="primary" loading={this.setState.loading} disabled={this.state.rowId==""} onClick={this.handleRow}>确定</Button>,
            <Button key="cancel" type="primary" loading={this.setState.loading} onClick={this.handleCancel}>取消</Button>,
          ]}>
          <div style={{ minHeight: '252px' }}>
            <div
            >
              <Table
                pagination={false}
                columns={columns}
                dataSource={this.state.tableData}
                rowClassName={this.setRowClassName}
                onRow={this.onClickRow}
                rowKey={record => record.key}
                // locale={{emptyText:this.state.findInfo}}
                style={{
                  marginTop: '12px', height: '496px',
                  overflow: 'auto'
                }}
              />
               <CommonPagination size={this.state.size} current={this.state.page} total={this.state.total} search={this.state.search}
                onShowSizeChange={this.onShowSizeChange.bind(this)} complete={true} dataSource={this.state.tableData}
                onChange={this.onChange.bind(this)} />
              {/* {
                this.state.tableData&&this.state.tableData.length>0?<Pagination
                showTotal={() => this.showTotal(this.state.total)}
                size="small"
                showQuickJumper
                showSizeChanger
                defaultCurrent={1}
                current={this.state.page ? this.state.page : 1}
                total={this.state.total ? this.state.total : 0}
                onChange={this.onChange}
                onShowSizeChange={this.onShowSizeChange}
                style={{ textAlign: "right", marginTop: '13px' }}
              />:""
            } */}
            </div>
          </div>
        </Modal>
        </div>
      )
    }
  })
export default TerminologyRAndNnaModals    